package cn.iocoder.yudao.module.system.service.patentapplication;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.patentapplication.vo.PatentApplicationNewRespVO;
import cn.iocoder.yudao.module.system.controller.admin.patentapplication.vo.PatentApplicationPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.patentapplication.vo.PatentApplicationSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.alluser.AllUserDO;
import cn.iocoder.yudao.module.system.dal.dataobject.patentapplication.PatentApplicationDO;
import cn.iocoder.yudao.module.system.dal.mysql.patentapplication.PatentApplicationMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.PATENT_APPLICATION_NOT_EXISTS;


/**
 * 会员专利申请 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PatentApplicationServiceImpl implements PatentApplicationService {

    @Resource
    private PatentApplicationMapper patentApplicationMapper;

    @Override
    public Long createPatentApplication(PatentApplicationSaveReqVO createReqVO) {
        // 插入
        PatentApplicationDO patentApplication = BeanUtils.toBean(createReqVO, PatentApplicationDO.class);
        patentApplicationMapper.insert(patentApplication);
        // 返回
        return patentApplication.getId();
    }

    @Override
    public void updatePatentApplication(PatentApplicationSaveReqVO updateReqVO) {
        // 校验存在
        validatePatentApplicationExists(updateReqVO.getId());
        // 更新
        PatentApplicationDO updateObj = BeanUtils.toBean(updateReqVO, PatentApplicationDO.class);
        updateObj.setUpdateTime(LocalDateTime.now());
        patentApplicationMapper.updateById(updateObj);
    }

    @Override
    public void deletePatentApplication(Long id) {
        // 校验存在
        validatePatentApplicationExists(id);
        // 删除
        patentApplicationMapper.deleteById(id);
    }

    private void validatePatentApplicationExists(Long id) {
        if (patentApplicationMapper.selectById(id) == null) {
            throw exception(PATENT_APPLICATION_NOT_EXISTS);
        }
    }

    @Override
    public PatentApplicationDO getPatentApplication(Long id) {
        return patentApplicationMapper.selectById(id);
    }

    @Override
    public PageResult<PatentApplicationDO> getPatentApplicationPage(PatentApplicationPageReqVO pageReqVO) {
        return patentApplicationMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<PatentApplicationNewRespVO> findPatentApplicationPage(Integer pageNum, Integer pageSize, String caseName,Long reviewerBy, Long userId) {
        IPage<PatentApplicationNewRespVO> patentApplicationPage = patentApplicationMapper.findPatentApplicationPage(new Page<>(pageNum, pageSize), caseName,reviewerBy,userId);
        PageResult<PatentApplicationNewRespVO> pageResult = new PageResult<>();
        pageResult.setList(patentApplicationPage.getRecords());
        pageResult.setTotal(patentApplicationPage.getTotal());
        return  pageResult;
    }

    @Override
    public PatentApplicationNewRespVO findPatentApplicationDetail(Long id) {
        return patentApplicationMapper.findPatentApplicationDetail(id);
    }

    @Override
    public void uploadReturnFiles(PatentApplicationSaveReqVO updateReqVO) {
        validatePatentApplicationExists(updateReqVO.getId());
        PatentApplicationDO patentApplicationDO = patentApplicationMapper.selectById(updateReqVO.getId());
        patentApplicationDO.setUploadDate(LocalDate.now());
        patentApplicationDO.setUpdateTime(LocalDateTime.now());
        patentApplicationDO.setReturnFiles(updateReqVO.getReturnFiles());
        patentApplicationMapper.updateById(patentApplicationDO);
    }

    @Override
    public void updateRemindTime(Long id) {
        validatePatentApplicationExists(id);
        PatentApplicationDO patentApplicationDO = patentApplicationMapper.selectById(id);
        patentApplicationDO.setRemindTime(LocalDate.now());
        patentApplicationDO.setUpdateTime(LocalDateTime.now());
        patentApplicationMapper.updateById(patentApplicationDO);
    }

}